from rest_framework.viewsets import ModelViewSet
from aaa import models
from rest_framework import serializers
from rest_framework.response import Response
import json
from django.http import JsonResponse
from aaa.utils.pager import myPageNumberPagination

class yySerializer(serializers.ModelSerializer):
    incidence = serializers.CharField(source='get_incidence_display')
    equipment_status = serializers.CharField(source='get_equipment_status_display')
    class Meta:
        model = models.yy1170
        fields = ['index','county','point','deviceID','deviceName','describle','proposer','incidence','deal',
                'solve_date','responsible_person','question_state','equipment_status','maintenance_date','pics',
                  'offline_date','belong']

class yysearchView(ModelViewSet):
    queryset = models.yy1170.objects.all()
    serializer_class = yySerializer
    authentication_classes = []

    def list(self, request, *args, **kwargs):
        print('args',request._request.GET.get('pagenum'),request._request.META)
        devices = self.get_queryset()
        # pg = myPageNumberPagination()
        # xx = pg.paginate_queryset(queryset=self.queryset,request=request,view=self)
        # ser = yySerializer(instance=xx,many=True)
        ser = yySerializer(instance=devices,many=True)
        ret = {}
        ret['data'] = json.dumps(ser.data)
        ret['total'] = self.queryset.count()
        return JsonResponse(ret)

    def update(self, request, *args, **kwargs):
        print('updateDevices res', request.data)
        data = request.data
        state = data['incidence']
        equipment_state = data['equipment_status']
        solve_date = data['solve_date']
        print('state000', type(state),state)
        if state == '1' or state == '红色预警':
            state = 1
        elif state == '2' or state == '橙色预警':
            state = 2
        elif state == '3' or state == '黄色预警':
            state = 3
        elif state == '4' or state == '蓝色预警':
            state = 4
        print('state111', state)
        # print('equipment_state 111', equipment_state,type(equipment_state))
        # if equipment_state == '4' or equipment_state == '在线':
        #     print('equipment_state 222', equipment_state)
        #     equipment_state = 4
        # elif equipment_state == '5' or equipment_state == '离线':
        #     print('equipment_state 333', equipment_state)
        #     equipment_state = 5
        try:
            obj = models.yy1170.objects.filter(index=data['index']).update(
                describle=data['describle'], proposer=data['proposer'], question_state=data['question_state'],
                incidence=state, responsible_person=data['responsible_person'], deal=data['deal'],maintenance_date=data['maintenance_date'],
                solve_date=solve_date,offline_date=data['offline_date'],equipment_status=equipment_state,pics=data['pics'])
            return JsonResponse({'meta': {'status': 201, 'msg': 'ok'}})
        except Exception as e:
            print('e',e)